package com.example.myapplication.tack_10;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.tack_10.adapter.ArrangeAdapter;
import com.example.myapplication.tack_10.adapter.FenLeiAdapter;
import com.example.myapplication.tack_10.data.ArrangeData;
import com.example.myapplication.tack_10.data.BannerData;
import com.example.myapplication.tack_10.data.FenLeiData;
import com.example.myapplication.utils.BannerTool;
import com.example.myapplication.utils.OkHttpTool;

import java.util.ArrayList;
import java.util.List;

public class GovernmentActivity extends AppCompatActivity {
    private ImageView exit;
    private List<BannerData.DataDTO> listBanner;
    private List<FenLeiData.RowsDTO> listFenLei;
    private List<ArrangeData.RowsDTO> listArrange;
    private ViewPager banner;
    private RecyclerView fenlei;
    private RecyclerView wdshuqiu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_10_activity_government);
        init();
        exit_();
        getBanner();
        getFenLei();
        getArrange();

    }

    private void getArrange() {
        String url = "prod-api/api/gov-service-hotline/appeal/list?pageNum=1&pageSize=10";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ArrangeData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                ArrangeData arrangeData = (ArrangeData)obj;
                if(arrangeData.getCode() == 200){
                    listArrange = arrangeData.getRows();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            arrangeAdapter();
                        }


                    });
                }

            }

            @Override
            public void onFailure(String msg) {
            }
        });
    }
    private void arrangeAdapter() {
        ArrangeAdapter arrangeAdapter = new ArrangeAdapter(listArrange, GovernmentActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(GovernmentActivity.this);
        wdshuqiu.setAdapter(arrangeAdapter);
        wdshuqiu.setLayoutManager(linearLayoutManager);

    }
    private void getFenLei() {
        String url = "prod-api/api/gov-service-hotline/appeal-category/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, FenLeiData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                FenLeiData fenLeiData = (FenLeiData)obj;
                if(fenLeiData.getCode() == 200){
                    listFenLei = fenLeiData.getRows();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            FenLeiAdapter();
                        }
                    });
                }

            }

            @Override
            public void onFailure(String msg) {
            }
        });
    }

    private void FenLeiAdapter() {
        FenLeiAdapter fenLeiAdapter = new FenLeiAdapter(listFenLei, GovernmentActivity.this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(GovernmentActivity.this,2);
        gridLayoutManager.setOrientation(GridLayoutManager.HORIZONTAL);
        fenlei.setAdapter(fenLeiAdapter);
        fenlei.setLayoutManager(gridLayoutManager);
    }

    private void getBanner() {
        String url = "prod-api/api/gov-service-hotline/ad-banner/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, BannerData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                BannerData bannerData = (BannerData)obj;
                if(bannerData.getCode() == 200){
                    listBanner = bannerData.getData();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayList<String> list = new ArrayList<>();
                            for (BannerData.DataDTO dataDTO : listBanner) {
                                list.add(dataDTO.getImgUrl());
                            }
                            BannerTool bannerTool = new BannerTool(GovernmentActivity.this,list,banner);
                            banner.setAdapter(bannerTool);
                        }
                    });
                }

            }

            @Override
            public void onFailure(String msg) {
            }
        });
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GovernmentActivity.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        banner = findViewById(R.id.banner);
        fenlei = findViewById(R.id.fenlei);
        wdshuqiu = findViewById(R.id.wdshuqiu);
    }
}